'use strict';
angular.module('app.auth').controller('LoginCtrl', function ($scope, $rootScope,$location, postService, $cookieStore,$http) {

    var opt=window.location.hash.replace("#/admin?","").split('&');
    // console.log(window.location.hash.replace("#/admin?",""))
    var param = {userid:"",roleid:"",rolename:"",code:""};
    for(var i in opt){
        var arr = opt[i].split("=");
        for(var name in param){
            if(name == arr[0]){
                param[name] = arr[1];
                if(name == 'rolename'){
                    param[name] = decodeURI(arr[1],'UTF-8');
                }
            }
        }
    }
    
    
    // console.log(param)
    if(window.location.hash.indexOf("?") >= 0){
        JumpInTo($http,param)
    }
    
    
    // console.log("param.userid="+param.userid,"cookie.user="+$cookieStore.get("user"))

    $scope.credentials = {
        id: param.userid == "" ? ($cookieStore.get("user") == undefined ? null : $cookieStore.get("user")) : param.userid,
        pass: '111111'
    };
    $scope.warning = '';
    
    $scope.loginPost = function () {
        //用户登录
        // console.log($scope.credentials.id)
        if($scope.credentials.id == null){
            $scope.warning = "抱歉，您的打开方式不对哦！";
        }else{
            $http.post(baseurls+"/userlogin",$scope.credentials,{withCredentials:true}).then(function(res){
                // console.log(res.data);
                if(res.data.ret == 1){//登陆成功
                  
                    $cookieStore.put("user",res.data.info.id);
                    $cookieStore.put("role",res.data.info.roleid);
                    $cookieStore.put("fid",res.data.info.fid);
    
                    //保存登陆日志
                    var data = {name:'success', info_name:$scope.credentials.id, info_func:'登陆', time: getNowFormatDate(), ip:returnCitySN["cip"]+','+returnCitySN["cname"]};
                    // console.log(data)
                    $http.post(baseurls+"/data/logfile",data).then(function(res){
                        // console.log(res.data);
                    })
    
                    window.location.href = appConfig.base[0].baseURI + "#/yk_data";//跳转至实时数据页面
    
                    
                        
                }else{
                    $scope.warning = "您的账号不存在！";
    
                }
            })
        }
        
    }

    
});